package com.xydtech.busiflw.controller;

import com.xydtech.busiflw.domain.BusiflInstStep;
import com.xydtech.busiflw.domain.BusiflWaitTask;
import com.xydtech.busiflw.service.IBusiflInstStepService;
import com.xydtech.busiflw.service.IBusiflWaitTaskService;
import com.xydtech.common.annotation.Log;
import com.xydtech.common.constant.FlowConstants;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 待办任务Controller
 *
 * @author JHON
 * @date 2021-06-11
 */
@Controller
@RequestMapping("/busiflw/waittask")
public class BusiflWaitTaskController extends BaseController {
    private String prefix = "busiflw/waittask";

    @Autowired
    private IBusiflWaitTaskService busiflWaitTaskService;
    @Autowired
    private IBusiflInstStepService busiflInstStepService;

    @RequiresPermissions("busiflw:waittask:view")
    @GetMapping()
    public String waitTask() {
        return prefix + "/waittask";
    }

    /**
     * 查询待办任务列表
     */
    @RequiresPermissions("busiflw:waittask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusiflWaitTask busiflWaitTask) {
        busiflWaitTask.setSts("1");
        busiflWaitTask.setBelongTo(ShiroUtils.getUserId());
        startPage();
        List<BusiflWaitTask> list = busiflWaitTaskService.selectBusiflWaitTaskList(busiflWaitTask);
        return getDataTable(list);
    }


    /**
     * 已提交任务
     */
    @RequiresPermissions("busiflw:waittask:view")
    @GetMapping("/hasSubmitTask")
    public String hasSubmitTask() {
        return prefix + "/hasSubmitTask";
    }

    /**
     * 查询已提交任务列表
     */
    @RequiresPermissions("busiflw:waittask:list")
    @PostMapping("/hasSubmitList")
    @ResponseBody
    public TableDataInfo hasSubmitList(BusiflWaitTask busiflWaitTask) {
        busiflWaitTask.setPerformer(ShiroUtils.getUserId());
        startPage();
        List<BusiflWaitTask> list = busiflWaitTaskService.selectHasSubmitTaskList(busiflWaitTask);
        return getDataTable(list);
    }

    /**
     * 已结束任务
     */
    @RequiresPermissions("busiflw:waittask:view")
    @GetMapping("/closedTask")
    public String closedTask() {
        return prefix + "/closedTask";
    }

    /**
     * 已结束任务步骤列表
     */
    @RequiresPermissions("busiflw:waittask:view")
    @GetMapping("/stepList/{versionId}/{instanceId}")
    public String stepList(@PathVariable("versionId") Long versionId, @PathVariable("instanceId") String instanceId, String flowName, ModelMap mmap) throws UnsupportedEncodingException {
        mmap.put("versionId", versionId);
        mmap.put("instanceId", instanceId);
        mmap.put("flowName", java.net.URLDecoder.decode(flowName, "UTF-8"));
        return prefix + "/taskStepList";
    }

    /**
     * 查询已结束任务步骤列表
     */
    @PostMapping("/taskStepList")
    @ResponseBody
    public TableDataInfo taskStepList(BusiflInstStep busiflInstStep) {
        startPage();
        List<BusiflInstStep> list = busiflInstStepService.selectBusiflInstStepList(busiflInstStep);
        return getDataTable(list);
    }

    /**
     * 查询已结束任务列表
     */
    @PostMapping("/closedTaskList")
    @ResponseBody
    public TableDataInfo closedTaskList(BusiflWaitTask busiflWaitTask) {
        busiflWaitTask.setBelongTo(ShiroUtils.getUserId());
        startPage();
        List<BusiflWaitTask> list = busiflWaitTaskService.selectCloseTaskList(busiflWaitTask);
        return getDataTable(list);
    }

    /**
     * 导出待办任务列表
     */
    @RequiresPermissions("busiflw:waittask:export")
    @Log(title = "待办任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusiflWaitTask busiflWaitTask) {
        List<BusiflWaitTask> list = busiflWaitTaskService.selectBusiflWaitTaskList(busiflWaitTask);
        ExcelUtil<BusiflWaitTask> util = new ExcelUtil<BusiflWaitTask>(BusiflWaitTask.class);
        return util.exportExcel(list, "waittask");
    }

    /**
     * 删除待办任务
     */
    @RequiresPermissions("busiflw:waittask:remove")
    @Log(title = "待办任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long taskId) {
        return toAjax(busiflWaitTaskService.deleteBusiflWaitTaskById(taskId));
    }

    /**
     * 检查待办任务是不是已被其他人处理或者删除,如果没有，分派策略为抢先处理则把其他人状态置为0-关闭
     */
    @Log(title = "检查待办任务", businessType = BusinessType.UPDATE)
    @PostMapping("/checkWaitTask")
    @ResponseBody
    public AjaxResult checkWaitTask(String instanceId, Long taskId) {
        BusiflWaitTask waitTask = busiflWaitTaskService.selectBusiflWaitTaskById(taskId);
        if (waitTask == null || FlowConstants.WAIT_TASK_STS_CLOSE.equals(waitTask.getSts())) {
            return error("当前任务已删除或已被其他人处理！");
        } else {
            //任务分派策略 1-任务抢先;其他人任务状态置为0-关闭
            if (waitTask.getTaskPolicy().equals(FlowConstants.TASK_POLICY_1)) {
                BusiflWaitTask busiflWaitTask = new BusiflWaitTask();
                busiflWaitTask.setBelongTo(ShiroUtils.getUserId());
                busiflWaitTask.setInstanceId(instanceId);
                busiflWaitTask.setSts("0");
                busiflWaitTask.setTaskBatchId(waitTask.getTaskBatchId());
                busiflWaitTaskService.updateOtherUserBusiflWaitTask(busiflWaitTask);
            }
        }
        return success();
    }

}
